<?php
/**
 * Created by PhpStorm.
 * User: Kau
 * Date: 2017/12/30
 * Time: 18:43
 */
namespace app\modules\controllers;

use app\modules\models\Admin;
use Yii;
use yii\web\Controller;

class PublicController extends Controller {

    /**
     * 管理员登录
     * @return string|\yii\web\Response
     * @throws \yii\base\ExitException
     */
    public function actionLogin() {
        $this->layout = false;
        if (Yii::$app->session['admin']['isLogin'] == 1) {
            return $this->redirect(['default/index']);
        }
        $model = new Admin;
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->login($post)) {
                $this->redirect(['default/index']);
                Yii::$app->end();
            }
        }

        return $this->render("login", ['model' => $model]);
    }

    /**
     * 管理员登出
     * @throws \yii\base\ExitException
     */
    public function actionLogout() {
        Yii::$app->session->removeAll();
        if (!isset(Yii::$app->session['admin']['isLogin'])) {
            $this->redirect(['public/login']);
            Yii::$app->end();
        }
        $this->goBack();
    }


    /**
     * 管理员找回密码
     * @return string
     */
    public function actionSeekpassword() {
        $this->layout = false;
        $model = new Admin();
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->seekPass($post)) {
                Yii::$app->session->setFlash('info','电子邮件已发送成功，请注意查收！');
            }
        }
        return $this->render("seekpassword", ['model' => $model]);
    }


}